#include <stdio.h>
#include <iostream>
#include "TcpSocket.h"
#include "json/json.h"
#include "jsoncpp.cpp"
using namespace std;
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif defined(_MSC_VER)
#pragma warning(disable : 4996)
#endif
TcpSocket client;
bool isRet = client.SocketInit();

int main()
{
    Json::Value coordinate;
    Json::Value position;
    Json::Value rotation;
    
    // position data
    position["x"] = 100.0;
    position["y"] = 100.0;
    position["z"] = 100.0;

    // rotation data
    rotation["a"] = 45;
    rotation["b"] = 0;
    rotation["c"] = 45;

    // set send Data
    coordinate["position"] = position;
    coordinate["rotation"] = rotation;
    coordinate["speed"] = 1000;
    
    string sendStr = coordinate.toStyledString(); // json to string
    printf(sendStr.c_str()); 

    if (!isRet)
        return 0;
    isRet = client.CreatSocket();
    string sendMsg = string();
    client.SendMsg(sendStr, sendMsg); 
    printf("server response: %s\n", sendMsg.c_str());
    client.StartReadRecvThread();
    while (1)
    {
        if (client.RecvMsg.length() > 0){
            Json::Value recvJson;
            Json::Reader reader;
            const char *jsonStr = client.RecvMsg.c_str();
            reader.parse(jsonStr, recvJson); // json to str
            if(recvJson.isMember("id")){
                printf("get id:%d\n", recvJson["id"].asInt()); // dome: { "id": 1 }
            }
            printf("loop read form client object, data source is actively sent by the server :%s\n", jsonStr);
        }
        Sleep(2000);
    }
    
    getchar();
    return 0;
}
